from itertools import pairwise
from typing import *


class Solution:

    def maxNonDecreasingLength(self, nums1: List[int],
                               nums2: List[int]) -> int:
        ans = n1 = n2 = 1
        for (a, b), (c, d) in pairwise(zip(nums1, nums2)):
            m1 = m2 = 1
            if c >= a:
                m1 = max(n1 + 1, m1)
            if c >= b:
                m1 = max(n2 + 1, m1)
            if d >= a:
                m2 = max(n1 + 1, m2)
            if d >= b:
                m2 = max(n2 + 1, m2)
            n1, n2 = m1, m2
            ans = max(n1, n2, ans)
        return ans


class Solution:

    def maxNonDecreasingLength(self, nums1: List[int],
                               nums2: List[int]) -> int:
        n = len(nums1)
        ans = 1
        n1 = n2 = 1
        for i in range(1, n):
            a, b, c, d = nums1[i - 1], nums2[i - 1], nums1[i], nums2[i]
            m1 = m2 = 1
            if c >= a:
                m1 = max(n1 + 1, m1)
            if c >= b:
                m1 = max(n2 + 1, m1)
            if d >= a:
                m2 = max(n1 + 1, m2)
            if d >= b:
                m2 = max(n2 + 1, m2)
            n1, n2 = m1, m2
            ans = max(n1, n2, ans)
        return ans